package com.firmboy.webservice.service;

import com.ufgov.safe.SafeUtil;
import com.ufgov.sm.SM2Utils;
import com.ufgov.sm.SM4Utils;
import com.ufgov.sm.util.Util;
import org.apache.axiom.om.OMElement;
import org.springframework.stereotype.Component;

import javax.jws.WebService;

/**
 * @author firmboy
 * @create 2018-05-16 上午11:46
 **/

@WebService(serviceName = "TestService",
        targetNamespace = "http://service.webservice.firmboy.com/",
        endpointInterface = "com.firmboy.webservice.service.TestService")
@Component
public class TestServiceImpl implements TestService{

    private static String ops_key = "0459BBCA5FFDBA7D048F410E298C225B0F27877053300D1A71C58A62E80AE3889B639CA22778DE882645DEBDD0A3F8AB688593260E6DD58EBB1C113A151563A950";

    @Override
    public String getXmlStr(String xmlStr, String sid, String appId, String msgType) {
        String sm4Xml = "";
        try{
            if(msgType.equals("A00101")){
                //获取sm4秘钥
                sm4Xml = getSm4Xml();
                sm4Xml = SM2Utils.encrypt(Util.hexToByte(ops_key), sm4Xml.getBytes());
                sm4Xml = SafeUtil.encode(sm4Xml, "utf-8");
            }
        }catch(Exception e){
            e.printStackTrace();
        }



        return sm4Xml;


    }

    public String getSm4Xml(){
        String sm4Key = SM4Utils.generateKey();

        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Root><Head><Version>v1.0</Version><MsgType>A00102</MsgType><Src>130</Src><Dst>00111101</Dst><MsgId>A001020011110011112017111600000173</MsgId><MsgRef>A001010011110011112017111600000173</MsgRef><SessionId>20170831110646200022</SessionId><PageMaxSize>1</PageMaxSize><PageCount>1</PageCount><PageNo>1</PageNo><RecordCount>1</RecordCount><WorkDateTime>20171116120000</WorkDateTime><Reserved></Reserved></Head><Body><Record><SM4>"+sm4Key+"</SM4></Record></Root>\n";
    }
}
